package fwk.display.particules
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.events.Event;
	import flash.utils.Dictionary;
	
	
	public class ParticuleAnim extends Bitmap
	{
		
		
		private var _clip:MovieClip;
		private static var _tabBitmaps:Dictionary;
		private var _currentBitmap:Bitmap;
		private var _currentIndex:int;
		
		private var _stage:Stage;
		
		
		public function ParticuleAnim(stage:Stage, clip:MovieClip, nbFrame:int, pwidth:int, pheight:int):void
		{
			
			if ( _tabBitmaps == null) {
				
				_tabBitmaps = new Dictionary();
			}
			
			_stage 					= stage;
			_clip 					= clip
			if (  _tabBitmaps[_clip] == null) {
				

				_tabBitmaps[_clip] 	= new Array();
				for (var i:int = 1; i <= nbFrame; i++) 
				{
					
					clip.gotoAndStop(i);
					addBitmap(clip, pwidth, pheight);
					
				}
			}
			
			_currentIndex = 0;
			super(_tabBitmaps[_clip][_currentIndex], "auto", true);
			
			
		}
		
		private function addBitmap(clip:MovieClip, pwidth:int = 1, pheight:int = 1):void
		{
			var bitmapData:BitmapData = new BitmapData(pwidth, pheight, true, 0x00000000);
			bitmapData.draw(clip, null, null, null, null, true);
			
			_tabBitmaps[_clip].push(bitmapData);
			
		}
		
		public function play(frameStart:int = 1):void
		{
			
			_currentIndex = frameStart -1;
			_stage.addEventListener(Event.ENTER_FRAME, onEnterFrameThis);
			

		}
		public function stop():void
		{
			_stage.removeEventListener(Event.ENTER_FRAME, onEnterFrameThis);
			

		}
		private function onEnterFrameThis(e:Event):void 
		{
			_currentIndex ++;
			if ( _currentIndex >= _tabBitmaps[_clip].length)
			{
				_currentIndex = 0;
			}
			
						
			renderBitmap();
			
			
		}
		private function renderBitmap():void 
		{
			bitmapData = _tabBitmaps[_clip][_currentIndex];

			
		}
		
	}
	
	
}

